import * as client from './client'
import urlConfig from './urlConfig'
import config from '../../config'
import * as rsaKey from './rsaKey'
import * as crypto from 'crypto'
import * as constants from 'constants'
///获取店铺信息
/**
 * 请求参数
 */
export class RegisterRst {
    /**
     * 注册信息
     */
    public registInfo: string;
    
}
/**
 * 返回数据
 */
export class RegisterRpn {
    //是否成功（1=>成功）
    public isSuccess: number;
    //状态码？
    public code: string;
    //描述
    public description: string;
    //返回数据
    public resData: ResData;
}

export class ResData{
    public tips: boolean;
    public userCode: string;
    
}

/**
 * 获取数据
 * @param rst 参数
 */
export async function exec(rst: RegisterRst) {
    /*
    if(config.openApi.enableRSA){
        let tmp = (await rsaKey.exec()).resData.publicKey;
        tmp = tmp.replace('-----BEGIN PUBLIC KEY-----','');
        tmp = tmp.replace('-----END PUBLIC KEY-----','');
        let begin = '-----BEGIN PUBLIC KEY-----\n';
        let end = '-----END PUBLIC KEY-----';
        let ret  = '';
        for(var i = 0; i < tmp.length; i += 64) {
          var a = tmp.slice(i, i + 64);
          ret  += (a + '\n');
        }
        let publicKey = begin + ret  + end;
        let keyParam = {"key": publicKey, padding: constants.RSA_PKCS1_PADDING};
        let plain = new Buffer(rst.registInfo);
        rst.registInfo = crypto.publicEncrypt(keyParam, plain).toString('base64');
        //console.log(publicKey,rst,rst.registInfo.length);
    }
    */
    const data = await client.exec(urlConfig.register, rst);
    return JSON.parse(data) as RegisterRpn;
}
